<?
	/**
	 * UCModAccount.php
	 * 点券、积分查询
	 *
	 * Copyright (c) 2010 snda.com - Web Development Center
	 * @author: Gu Shougang <gushougang@snda.com>
	 */
	
	if(__UC_ENV == 'DEV')
	{
		define('JIFEN_MERCHANTID', '000006');
		define('JIFEN_KEY', 'test');
		define('JIFEN_SEP', '|');
		define('JIFEN_WSDL', 'http://61.172.247.108/jf24/ExchangeScore/ExchangeService.asmx?WSDL');
		define('WSDL_FILE', '/tmp/ExchangeService.wsdl');
		define('DIANQUAN_WSDL', 'http://61.172.251.5/billingwsweb/billingws?WSDL');
		define('PAYTYPE', 2);
		define('DIANQUANTYPE', 3);
		define('JIFENTYPE', 6);
		define('UC_PAYMENT_WSDL_CACHE', 0);
	}
	else
	{
		define('JIFEN_MERCHANTID', '140037');
		define('JIFEN_KEY', 'UserCenter2010@Jifen');
		define('JIFEN_SEP', '|');
		define('JIFEN_WSDL', 'http://jf.sdo.com/ExchangeScore/ExchangeService.asmx?WSDL');
		define('WSDL_FILE', '/tmp/ExchangeService.wsdl');
		define('DIANQUAN_WSDL', 'http://61.172.252.231:7777/billingwsweb/billingws?WSDL');
		define('PAYTYPE', 2);
		define('DIANQUANTYPE', 3);
		define('JIFENTYPE', 6);
		define('UC_PAYMENT_WSDL_CACHE', 300);
	}
	
	require_once(dirname(__FILE__) . '/../../libraries/UCLibWebService.php');
	
	class UCModAccount
	{		
		/**
		 * 构造函数
		 * 调用__construct()
		 */
		public function UCModAccount()
		{
			$this->__construct();
		}
		
		/**
		 * 构造函数
		 */
		public function __construct()
		{
			// Do nothing
		}
		
		/**
		 * 获取点券余额
		 * @param string $ptid
		 * @return int/false
		 */
		public static function getAccountInfo($ptid)
		{		
			if(isset($_SESSION['accountInfo']) && is_array($_SESSION['accountInfo']))
			{
				if(time() - $_SESSION['accountInfo']['last_update'] <= UC_PAYMENT_WSDL_CACHE)
				{
					return $_SESSION['accountInfo']['value'];
				}
			}
			
			$accountInfo = array(
				'dianquan' => self::_getRemainCoin($ptid, PAYTYPE, DIANQUANTYPE, $message),
				'jifen' => self::_getRemainCoin($ptid, PAYTYPE, JIFENTYPE, $message),
			);
			
			$_SESSION['accountInfo'] = array(
				'value' => $accountInfo,
				'last_update' => time(),
			);
			
			return $accountInfo;
		}
		
		/**
		 * 获取积分值: 作废 By Gu Shougang 2010-5-14 10:19
		 *
		 * @param string 
		 */
		private function _getRemainPoint($ptid, &$message)
		{
			//$ptid = 'jftest4';
			
			$param['userID'] = $ptid;
			$param['merchantID'] = JIFEN_MERCHANTID;
			$queryTime = time();
			$param['queryTime'] = $queryTime;
			$param['encryptCode'] = strtoupper(md5($ptid.JIFEN_SEP.JIFEN_MERCHANTID.JIFEN_SEP.JIFEN_KEY));
						
			if(self::_check_wsdl() === FALSE)
			{
				return '';
			}
			
			try
			{
				$soap = new SoapClient(WSDL_FILE);
				//var_dump($soap->__getFunctions());
				
				$r = $soap->querySNDAScore($param);
			}
			catch(SoapFault $e)
			{
				$message = $e->getMessage();				
				return '';
			}
			
			if($r === FALSE)
			{
				return '';
			}
			
			if($r->querySNDAScoreResult->RetCode < 0)
			{
				return '';
			}
			
			return $r->querySNDAScoreResult->RetCode;
		}
		
		/**
		 * 检查WSDL文件
		 *
		 * @return boolean
		 */
		private function _check_wsdl()
		{
			if(!file_exists(WSDL_FILE))
			{
				$wsdlh = fopen(WSDL_FILE, 'w');
				
				$ua = array('Mozilla','Opera','Microsoft Internet Explorer','ia_archiver');
				$op = array('Windows','Windows XP','Linux','Windows NT','Windows 2000','OSX');
				$agent = $ua[rand(0,3)].'/'.rand(1,8).'.'.rand(0,9).' ('.$op[rand(0,5)].' '.rand(1,7).'.'.rand(0,9).'; en-US;)';

				$ch = curl_init();
				curl_setopt($ch, CURLOPT_TIMEOUT, 3);
				curl_setopt($ch, CURLOPT_URL, JIFEN_WSDL);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($ch, CURLOPT_USERAGENT, $agent);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
				$wsdl_content=curl_exec($ch);
				curl_close($ch);
				
				if(fwrite($wsdlh, $wsdl_content) === FALSE)
				{
					return FALSE;
				}
				fclose($wsdlh);
			}
			
			return TRUE;
		}
		
		/**
	      * 获取余额
	      *
	      * @param string $balanceInfo
	      * @param string $message
	      * @return int|boolean
	      */
		private function _getRemainCoin($ptid, $indicationAuthen, $payType, &$message)
		{
			//$ptid = 'nijia';
			
			$param['uidType'] = 1;
			$param['userId'] = $ptid;
			$param['spId'] = 1;
			$param['areaIdPlayer'] = 0;
			$param['servIdPlayer'] = 0;
			$param['orderId'] = md5(microtime(TRUE) . rand(10000, 99999));
			$param['endpointIp'] = get_client_ip();
			$param['callTime'] = date('Y-m-d H:i:s');
			$param['indicationAuthen'] = $indicationAuthen;
			# 3 盛大点券
			$param['payType'] = $payType;
			$param['coupleType'] = 1;
			# 1 PC ,2 Mobile
			$param['endpointType'] = 1;
			# 1 PC ,2 EZHOME
			$param['platformType'] = 1;
			# unknown param
			$param['groupIdPlayer'] = 0;
			$param['reserveInt'] = 1;
			$param['reserveString'] = 1;
			$param['userType'] = 1;
			
			$option['soap_version'] = 'SOAP_1_1';
			$option['location'] = trim(DIANQUAN_WSDL, '?WSDL');
			
			$r = UCLibWebService::soapRequest('accountAuthen', $param, DIANQUAN_WSDL, $option, $message);
			
			if($r === FALSE)
			{
				return '';
			}
			
			if($r->result !== 0)
			{
				if($r->result == -106908)
				{
					return 0;
				}
				else
				{
					$message = self::_authenError($r->result, $message);					
					return '';
				}
			}
			
			$remain = explode('|', $r->balanceInfo, '2');
			if(!isset($remain[1]))
			{
				$message = '余额获取错误';
				return '';
			}
			else
			{
				return (int)$remain[1];
			}
		}
		
		/**
		 * 余额错误信息
		 *
		 * @param int $id
		 * @return string
		 */
		private function _authenError($id)
		{
			$error = array(
				- 106901 => '传入数字账号格式错误' , 
				- 106902 => '传入PT格式错误' , 
				- 106903 => '传入游戏不存在' , 
				- 106904 => '传入游戏区不存在' , 
				- 106905 => '该游戏没有配置有效认证paytypeid,或配置错误' , 
				- 106906 => '该游戏认证paytypeid配置错误' , 
				- 106907 => '对应的paytypeid没有配置账号类型' , 
				- 106908 => '该账号没有余额或对应账号不存在' , 
				- 106910 => '查找总账号元宝的汇率转换关系出错(rateex配置问题)' , 
				- 106911 => '传入的耦合类型不支持' , 
				- 106912 => '传入的paytypeid不支持' , 
				- 106913 => 'AION时长不支持全区认证' , 
				- 106914 => 'TBAIONDEPOSITCFG表配置信息不存在' , 
				- 106915 => '根据TBAIONDEPOSITCFG表配置信息建立数据库连接失败' , 
				- 106916 => 'AION账号不存在' , 
				- 106917 => 'AION时长认证indication只能是4' , 
				- 106918 => '进行数据库操作的时候发生异常' , 
				- 106997 => '传入参数存在空值' , 
				- 106998 => '传入参数超出最大长度限制或为空' , 
				- 106999 => '数据库异常' , 
				- 1 => '未知错误',
			);
			
			if(isset($error[$id]))
			{
				return $error[$id];
			}
			return $error[- 1];
		}
	}
?>